Retro-programming using Acme::Gosub

Shlomi Fish on 2005-07-20T10:11:59

Yesterday I uploaded the first public release of Acme::Gosub to CPAN. This allows you to emulate BASIC's "GOSUB" and "RETURN" statements in Perl. It all started a long time ago when I thought that Perl's "goto EXPR" statement was powerful enough to emulate goto, by keeping a stack of labels, and where RETURN would be something like goto pop(@labels).

Then I came to the idea of writing a module to do it. I searched CPAN for gosub but couldn't find anything relevant so the coast was clear. However, I then got stucked up in a technicality. A few days ago, I was able to find a way to resolve it, and so set down to write the module.

I started from Switch.pm which I knew utilized a code filter. My needs were simpler except for trying to have a "gosub EXPR;" as a statement, for which I tricked Text::Balanced into doing it by prepending a leading semicolon and then asking to find a match code-block that was delimited by semi-colons. (it's a kludge, but it works).

I finished it in one afternoon, and then uploaded it. It has a nice test-suite with some funky functions that make use of gosub. At the moment, it has some glaring bugs and limitations, but hopefully they will be resolved in a future work.

So now I give you the Acme::Gosub JAPH:

#!/usr/bin/perl

use strict;
use warnings;

use Acme::Gosub;

my @japh = ("Just another", " Perl", " and Acme::Gosub", " Hacker\n");

sub print_japh
{
    my $print_me;
    $print_me = $japh[0];
    gosub PRINT;
    $print_me = $japh[1];
    gosub PRINT;
    $print_me = $japh[2];
    gosub PRINT;
    $print_me = $japh[3];
    gosub PRINT;
    return;
    PRINT:
    print $print_me;
    greturn;
}
print_japh();